package com.yc.framework.web.exception;

import com.yc.common.annotation.Log;
import com.yc.common.constant.HttpStatus;
import com.yc.common.core.domain.AjaxResult;
import com.yc.common.exceptions.captcha.CaptchaException;
import com.yc.common.exceptions.captcha.CaptchaExpireException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author YanChen
 * @Date 2025/4/18 6:51
 **/
@RestControllerAdvice
@Slf4j
public class CaptchaExceptionHandler {

    /**
     * 验证码错误异常
     */
    @Log
    @ExceptionHandler(CaptchaException.class)
    public AjaxResult handleCaptchaException(CaptchaException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',验证码错误: {}", requestURI, e.getMessage());
        return AjaxResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
    }

    /**
     * 验证码过期异常
     */
    @Log
    @ExceptionHandler(CaptchaExpireException.class)
    public AjaxResult handleCaptchaExpireException(CaptchaExpireException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.warn("请求地址'{}',验证码已过期: {}", requestURI, e.getMessage());
        return AjaxResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
    }
}
